using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace dender.dvdcover.core
{
    /// <summary>
    /// Defines a PrintDocument for a cover
    /// </summary>
    public class CoverPrintDocument : PrintDocument
    {
        const float scalingFactor = 10 / 2.54f;
        private Cover mCover;
        /// <summary>
        /// Creates a CoverPrintDocument by specifying the cover
        /// </summary>
        /// <param name="cover">Cover of which the PrintDocument will be created</param>
        public CoverPrintDocument(Cover cover, PaperKind paperKind)
            : base()
        {
            mCover = cover;
            this.PaperKind = paperKind;
            this.DefaultPageSettings.Landscape = cover.Format.PageOrientation == PageOrientation.Landscape;
        }

        public PaperKind PaperKind
        {
            get
            {
                return this.DefaultPageSettings.PaperSize.Kind;
            }
            set
            {
                this.DefaultPageSettings.PaperSize = GetPaperSize(PaperKind.A4);
            }
        }

        private PaperSize GetPaperSize( PaperKind kind)
        {
            PaperSize returnSize = null;
            for (int i = 0; i < this.PrinterSettings.PaperSizes.Count; i++)
            {
                if (this.PrinterSettings.PaperSizes[i].Kind == kind)
                {
                    returnSize = this.PrinterSettings.PaperSizes[i];
                }
            }
            if (returnSize != null)
            {
                return returnSize;
            }
            else
            {
                return this.DefaultPageSettings.PaperSize;
            }
        }

        /// <summary>
        /// Defines how to print the cover
        /// </summary>
        /// <param name="e">PrintPageEventArgs</param>
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);
            Graphics printer = e.Graphics;

            foreach (Part p in mCover.Parts)
            {
                Rectangle destination = new Rectangle(
                    new Point((int)(p.Location.X*scalingFactor),(int)(p.Location.X * scalingFactor)),
                    new Size((int)(p.Width * scalingFactor), (int)(p.Height * scalingFactor)));
                if (p.Image != null)
                {
                    printer.DrawImage(p.Image.Image, destination);
                }
            }

            foreach (CuttingLine c in mCover.CuttingLines)
            {
                PointF p1 = c.Point1;
                p1.X *= scalingFactor;
                p1.Y *= scalingFactor;

                PointF p2 = c.Point2;
                p2.X *= scalingFactor;
                p2.Y *= scalingFactor;
                //Make this custom
                Pen myPen = new Pen(Brushes.DarkGray);
                myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                myPen.Width = 1.0f;
                printer.DrawLine(myPen, p1, p2);
            }
        }

    }
}
